package com.baijiayun.liveuibase.widgets.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;

import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatImageView;
import android.util.AttributeSet;

import com.baijiayun.liveuibase.R;


public class CheckImageView extends AppCompatImageView {

    private Drawable checkedImg;
    private Drawable unCheckedImg;
    private boolean isChecked;
    private OnCheckedChangeListener listener;
    private static final int[] STATE_CHECKED = {R.attr.bjlive_state_checked_img};
    private boolean stateChecked;

    public CheckImageView(Context context) {
        this(context,null);
    }

    public CheckImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CheckImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BJYCheckImageView);
        isChecked = typedArray.getBoolean(R.styleable.BJYCheckImageView_bjlive_is_checked,false);
        int checkedImgResId = typedArray.getResourceId(R.styleable.BJYCheckImageView_bjlive_check_img, 0);
        int unCheckedImgResId = typedArray.getResourceId(R.styleable.BJYCheckImageView_bjlive_uncheck_img, 0);
        if (checkedImgResId != 0) {
            checkedImg = AppCompatResources.getDrawable(context, checkedImgResId);
        }
        if (unCheckedImgResId != 0) {
            unCheckedImg = AppCompatResources.getDrawable(context, unCheckedImgResId);
        }
        typedArray.recycle();
        setCheckedDrawable();
    }


    /**
     * 设置选中或者未选中状态
     */
    public void setChecked(boolean isChecked){
        this.isChecked = isChecked;
        setCheckedDrawable();
        if(listener != null){
            listener.onCheckedChanged(isChecked);
        }
    }

    /**
     * 设置选中状态背景图片
     */
    public void setCheckedDrawable(Drawable drawable) {
        this.checkedImg = drawable;
        setCheckedDrawable();
    }

    public void setUnCheckedDrawable(Drawable drawable) {
        this.unCheckedImg = drawable;
        setCheckedDrawable();
    }

    /**
     * 设置非选中状态背景图片
     */
    private void setCheckedDrawable(){
        if(isChecked){
            setImageDrawable(checkedImg);
        }else{
            setImageDrawable(unCheckedImg);
        }
    }

    public Drawable getUnCheckedImg(){
        return unCheckedImg;
    }

    /**
     * 返回按钮当前选中状态
     */
    public boolean isChecked(){
        return isChecked;
    }

    /**
     * 设置监听
     */
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener){
        this.listener = listener;
    }
    public void setStateChecked(boolean stateChecked) {
        if (this.stateChecked != stateChecked) {
            this.stateChecked = stateChecked;
            refreshDrawableState();
        }
    }


    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        if (stateChecked) {
            final int[] drawableState = super
                    .onCreateDrawableState(extraSpace + 1);
            mergeDrawableStates(drawableState, STATE_CHECKED);
            return drawableState;
        }
        return super.onCreateDrawableState(extraSpace);
    }

    /**
     * 状态改变的监听
     */
    public interface OnCheckedChangeListener{
        void onCheckedChanged(boolean isChecked);
    }
}
